﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Text;
using NHibernate.Cfg;
using NHibernate;
using log4net;
using NHibernate.Criterion;
using BidFunLib.Entity;
using BidFunLib.DAO;
using System.Diagnostics;


namespace BidFunLib
{
    /// <summary>
    /// 
    /// </summary>
    class BidOperatorTypeDAO 
    {
        /// <summary>
        /// logger of this class
        /// </summary>
        private static readonly ILog log = LogManager.GetLogger(typeof(BidOperatorTypeDAO));

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pseudo"></param>
        /// <returns></returns>
        public BidOperatorType getBidOperatorTypeById(BidFunLib.Services.BidOperatorTypeManager.BID_OPERATOR_TYPE pseudo)
        {
            BidOperatorType bidOperatorType = null;

            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                IList<BidOperatorType> bidOperatorTypes = (IList<BidOperatorType>)session.CreateCriteria(typeof(BidOperatorType)).Add(Expression.Like("Name", pseudo.ToString())).List<BidOperatorType>();
                if (bidOperatorTypes != null && bidOperatorTypes.Count > 0)
                {
                    bidOperatorType = bidOperatorTypes.First();
                }
                transaction.Commit();
                session.Close();
                return bidOperatorType;
            }
        }

        /// <summary>
        /// Save the object 
        /// </summary>
        public BidOperatorType saveBidOperatorType(BidOperatorType bidObject)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                log.Debug("Saving BidOperatorType " + bidObject.Name);
                Int32 newID = (Int32)session.Save(bidObject);
                bidObject.IdBidOperatorType = newID;
                log.Debug("new BidOperatorType " + bidObject.Name + " saved !");
                transaction.Commit();
                session.Close();

                return bidObject;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public BidOperatorType getBidOperatorTypeByName(String name)
        {
            Debug.WriteLine("DAO : getBidOperatorTypeByName : " + name);
            BidOperatorType bidOperatorType = null;

            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                IList<BidOperatorType> opeType = (IList<BidOperatorType>)session.CreateCriteria(typeof(BidOperatorType)).Add(Expression.Like("Name", name)).List<BidOperatorType>();
                if (opeType != null && opeType.Count > 0)
                {
                    bidOperatorType = opeType.First();
                }
                transaction.Commit();
                session.Close();
                return bidOperatorType;
            }
        }
    }
}
